<?php
namespace app\admin\controller;
use app\admin\model\ActivityModel;
use think\Controller;
use \think\Request;


class  Activity extends Controller
{
    /*
     *活动列表
     */
    public function index(){
        $data = [
            'active_title'=>input('active_title')
        ];

        $model = new ActivityModel();
        $res = $model->selectAll($data);
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    /*
     *添加活动内容显示
     */
    public function addActivityView(){

        return $this->fetch('add_activity');
    }

    /*
     *添加活动内容（操作）
     */
    public function addActivityList(){
        $time = new ActivityModel();
        $time = $time->getSelectTime();
        if($time['start_time'] > time()){
            return 'time';
        }
        $start_time = strtotime(input('start_time'));
        $over_time = strtotime(input('over_time'));
        $data = [
            'active_title' => input('active_title'),
            'active_content' => input('active_content'),
            'active_imgs_url' => input('active_imgs_url'),
            'start_time' => $start_time,
            'over_time' => $over_time,
            'create_time' => time()
        ];
        $model = new ActivityModel();
        $row = $model->insertRow($data);
        return $row;
    }

    /*
     *上传图片
     */
    public function addActivity(){
        if(Request::instance()->isPost()){
            $file = request()->file('img');
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'activity');

            if($info){
                echo $path ='upload'.'/'.'activity'.'/'.date('Ymd').'/'.$info->getFilename();
            }else{
                echo $file->getError();
            }
        }
    }
    /*
     *修改内容显示
     */
    public function updateActivityView(){
        $id = input('id');
        $model = new ActivityModel();
        $row = $model->selectFind($id);
        $this->assign('row',$row);
        return $this->fetch('update_activity');
    }
    /*
     *修改内容（操作）
     */
    public function updateActivityList(){
        $start_time = strtotime(input('start_time'));
        $over_time = strtotime(input('over_time'));
        $data = [
            'id' =>input('id'),
            'active_title' => input('active_title'),
            'active_content' => input('active_content'),
            'active_imgs_url' => input('active_imgs_url'),
            'start_time' => $start_time,
            'over_time' => $over_time,
            'create_time' => time()
        ];
        $model = new ActivityModel();
        $row = $model->saveFind($data);
        return $row;
    }

    /*
     *育儿知识内容单个删除（逻辑）
     */
    public function delActivity(){
        $id = input('id');
        $model = new ActivityModel();
        $res = $model->deleteFind($id);
        return $res;
    }


}